﻿<?php
/**
 * @author          by Kelvin
 * @mail            dangtrungkien89@gmail.com
 * @copyright       Bidiots.net
 * @version         1.0
 * */
class StringHelper{
    var $vnarray = array(  
            'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',  
            'd'=>'đ',  
            'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',  
            'i'=>'í|ì|ỉ|ĩ|ị',  
            'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',  
            'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',  
            'y'=>'ý|ỳ|ỷ|ỹ|ỵ',  
            'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',  
            'D'=>'Đ',  
            'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',  
            'I'=>'Í|Ì|Ỉ|Ĩ|Ị',  
            'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',  
            'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',  
            'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',  
        ); 
	var $lower = '
			a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z
			|á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ
			|đ
			|é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ
			|í|ì|ỉ|ĩ|ị
			|ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ
			|ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự
			|ý|ỳ|ỷ|ỹ|ỵ';
	var $upper = '
			A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
			|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ
			|Đ
			|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ
			|Í|Ì|Ỉ|Ĩ|Ị
			|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ
			|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự
			|Ý|Ỳ|Ỷ|Ỹ|Ỵ';

    //Chuyển đổi hoa/thường trong tiếng việt - unicode
	function ToLower($str){
		$arrayUpper = explode('|',preg_replace("/\n|\t|\r/","",$this->upper));
		$arrayLower = explode('|',preg_replace("/\n|\t|\r/","",$this->lower));
		return str_replace($arrayUpper,$arrayLower,$str);
	}
	function ToUpper($str){
		$arrayUpper = explode('|',preg_replace("/\n|\t|\r/","",$this->upper));
		$arrayLower = explode('|',preg_replace("/\n|\t|\r/","",$this->lower));
		return str_replace($arrayLower,$arrayUpper,$str);
	}
 
   //Gỡ bỏ dấu tiếng việt
   function RemoveSign($str){
     foreach($this->vnarray as $en=>$uni){  
         $str = preg_replace("/($uni)/i", $en, $str);  
     }  
     return $str; 
   }
    
    //Chuyển chuỗi title bài viết về dạng có thể rewrite url
    //ví dụ http://vnexpress.net/gl/the-gioi/2012/02/tuong-nga-chien-tranh-iran-dien-ra-truoc-mua-he/
    function ToUrlRewrite($text){
        $text = $this->RemoveSign($text);
        $text = $this->ToLower($text);
        $text = preg_replace("/\W/", ' ', $text);
        $text = preg_replace("/\s+/", ' ', $text);
        return strtolower(preg_replace("/\s+/", '-', trim($text)));
    }
    
    function GetLength($str){
        return strlen($str);
    }
    
    function RegExEscape($string){
        return str_replace('/', '\/', str_replace("\\", "\\\\", $string));
    }
    
    function RemoveSpace($text){
        return preg_replace("/\s+/", ' ', trim($text));
    }
    function EscapeString($string){
        return addslashes(trim($string));
    }
    
   
//Tìm kiếm, chuỗi con 
   function Contains($needle, $string, $case_different = false){
      if (!$needle){
         return true;
      }
      if(!$case_different){
         return stripos($string, $needle) !== false;
      }
      else return preg_match("/.*$needle.*/",$string);
      
	}
   function EndWith($end, $string, $case_different = false){
      $end = self::RegExEscape($end);
      if($case_different){
         return preg_match("/$end$/", $string);   
      }
      else{
         $end = $this->ToLower($end);
         $string = $this->ToLower($string);
         return preg_match("/$end$/", $string);
      }
      
   }
   function BeginWith($begin, $string, $case_different = false){
      $begin = self::RegExEscape($begin);
      return preg_match("/^$begin/", $string);
	}
   function SubString($str, $start, $length){
      return substr($str,$start,$length);
   }
    
//Remove, crop, cut  
   function Replace($remove, $str){
      return str_replace($remove, null, $str);
   }
   function Trim($str,$trim){
      return trim($str,$trim);
   }
   function CropWord($text, $words = 50){
		$matches = preg_split("/\s+/", $text, $words + 1);
		$sz = count($matches);
		if ($sz > $words){
			unset($matches[$sz-1]);
			return implode(' ',$matches)."...";
		}
		return $text;
	}
   function Crop($text, $length = 100, $reserve_last_word = false){
      if (strlen($text)>$length){
          if ($reserve_last_word){
              $text = substr($text, 0, $length);
              $text = substr($text, 0, strrpos($text, ' ')).'...';
          }
          else{
              $text = substr($text, 0, $length).'...';
          }
      }

      return $text;
	} 
   function RemoveBlankLine($str){
		return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $str);
	}
//Check, kiểm tra, chuyển đổi
   function ToHTML($text){
		$text = htmlentities($text);
		$text = preg_replace("/\r\n/", "\n", $text);
		return preg_replace("/\n/", "<br />", $text);
	}
    
    //Kiểm tra email hợp lệ
   function CheckValidEmail($email){
      return preg_match("/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $email);
   }
}
?>